package com.freesid.ask.web;

import java.awt.image.BufferedImage;
import java.util.Date;
import java.util.Properties;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.util.Config;

@Controller
public class CodeImageController {

	private static Log log = LogFactory.getLog(CodeImageController.class);

	private Properties props = new Properties();

	private Producer kaptchaProducer = null;

	private String sessionKeyValue = null;

	private String sessionKeyDateValue = null;
	
	public static String SESSION_KEY_VALUE = CodeImageController.class.getName() + "_SESSION_KEY_VALUE" ;
	
	public static String SESSION_KEY_DATE_VALUE = CodeImageController.class.getName() + "_SESSION_KEY_DATE_VALUE" ;

	public CodeImageController() {
		ImageIO.setUseCache(false);
		props.put("kaptcha.image.width", "150");
		props.put("kaptcha.image.height", "50");
//		props.put("kaptcha.textproducer.char.string", "0123456789ABCDED");
		props.put("kaptcha.textproducer.char.length", "4");
		
		Config config = new Config(this.props);
		this.kaptchaProducer = config.getProducerImpl();
//		this.sessionKeyValue = config.getSessionKey();
		this.sessionKeyValue = SESSION_KEY_VALUE ;
//		this.sessionKeyDateValue = config.getSessionDate();
		this.sessionKeyDateValue = SESSION_KEY_DATE_VALUE ;
	}

	@RequestMapping("/code.jpeg")
	public void image(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		// Set to expire far in the past.
		resp.setDateHeader("Expires", 0);
		// Set standard HTTP/1.1 no-cache headers.
		resp.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
		// Set IE extended HTTP/1.1 no-cache headers (use addHeader).
		resp.addHeader("Cache-Control", "post-check=0, pre-check=0");
		// Set standard HTTP/1.0 no-cache header.
		resp.setHeader("Pragma", "no-cache");

		// return a jpeg
		resp.setContentType("image/jpeg");

		// create the text for the image
		String capText = this.kaptchaProducer.createText();

		if (log.isDebugEnabled()) {
			log.debug("capText:" + capText);
		}

		// store the text in the session
		req.getSession().setAttribute(this.sessionKeyValue, capText);

		// store the date in the session so that it can be compared
		// against to make sure someone hasn't taken too long to enter
		// their kaptcha
		req.getSession().setAttribute(this.sessionKeyDateValue, new Date());

		// create the image with the text
		BufferedImage bi = this.kaptchaProducer.createImage(capText);

		ServletOutputStream out = resp.getOutputStream();

		// write the data out
		ImageIO.write(bi, "jpg", out);
		try {
			out.flush();
		} finally {
			out.close();
		}
	}

}
